package com.xiaoran.rabbitmq.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.image.DirectColorModel;
import java.util.HashMap;
import java.util.Map;

///TTL配置类注解
@Configuration
public class TTlQueueConfig {
    public static final String x_exchange="X";
    public static final String A_queue="QA";
    public static final String B_queue="QB";
    public static final String C_queue="QC";
    public static final String y_Dead_exchange="Y";
    public static final String y_dead_queue="QD";

    ///声明普通交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
      return new DirectExchange(x_exchange);
    };
    //声明延迟交换机
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(y_Dead_exchange);
    };


    //声明普通队列 A ,延迟过期时间为10S
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments=new HashMap<>();
        //设置我们的死信交换机
        arguments.put("x-dead-letter-exchange",y_Dead_exchange);
        //设置我们的死信队列的  RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置我们的过期时间
        arguments.put("x-message-ttl",10000);
        return QueueBuilder.durable(A_queue).withArguments(arguments).build();
    };
    //声明普通队列 B
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments=new HashMap<>();
        //设置我们的死信交换机
        arguments.put("x-dead-letter-exchange",y_Dead_exchange);
        //设置我们的死信队列的  RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置我们的过期时间
        arguments.put("x-message-ttl",40000);
        return QueueBuilder.durable(B_queue).withArguments(arguments).build();
    };
    //声明普通队列 C
    @Bean("queueC")
    public Queue queueC(){
        Map<String, Object> arguments=new HashMap<>();
        //设置我们的死信交换机
        arguments.put("x-dead-letter-exchange",y_Dead_exchange);
        //设置我们的死信队列的  RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        return QueueBuilder.durable(C_queue).withArguments(arguments).build();
    };

    //声明延迟队列 D
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(y_dead_queue).build();
    };

    //交换机与队列的绑定   A
    @Bean
    public Binding queueA_Binding_X(@Qualifier("queueA") Queue QueueA,@Qualifier("xExchange") DirectExchange XExchange){
        return BindingBuilder.bind(QueueA).to(XExchange).with("XA");
    }

    //交换机与队列的绑定   B
    @Bean
    public Binding queueB_Binding_X(@Qualifier("queueB") Queue QueueB,@Qualifier("xExchange") DirectExchange XExchange){
        return BindingBuilder.bind(QueueB).to(XExchange).with("XB");
    }
    //交换机与队列的绑定  C
    @Bean
    public Binding queueC_Binding_X(@Qualifier("queueC") Queue QueueC,@Qualifier("xExchange") DirectExchange XExchange){
        return BindingBuilder.bind(QueueC).to(XExchange).with("XC");
    }


    //交换机与队列的绑定   D
    @Bean
    public Binding queueD_Binding_Y(@Qualifier("queueD") Queue QueueD,@Qualifier("yExchange") DirectExchange YExchange){
        return BindingBuilder.bind(QueueD).to(YExchange).with("YD");//这里我写错了，相信自己，持之以恒
    }


}
